<?php
require_once(DEDEINC.'/datalistcp.class.php');
require_once(DEDEINC.'/enums.func.php');
require_once(DEDEINC.'/arc.partview.class.php');
class collegeinfo_detail extends Control{

    function collegeinfo_detail(){
        parent::__construct();
        $this->mcollegeinfo_detail = $this->Model('mcollegeinfo_detail');
    }

    function ac_index(){

        // 处理cookie获取最近浏览和推荐院校的数组
        // by HehongChen
        // 2014.10.31
        // 通过cookie获取最近浏览的院校的id
        $history_id_pre = rtrim($_COOKIE['history_id'],',');
        $history_ids = explode(',', $history_id_pre);
        $history_ids_str = join(',',$history_ids);
        // 获取最佳浏览的院校的数组
        $sql = "SELECT * FROM `#@__collegeinfo` WHERE  collegeId IN ($history_ids_str)";
        // var_dump($this->dsql);exit();
        $this->dsql->SetQuery($sql);
        $this->dsql->Execute();
        $history_college = array();
        while($row = $this->dsql->GetArray()){
            $history_college[] = $row;
        }
        // 推荐院校的数组
        $recommend_college = self::CollegeRecommend($history_ids);



        // schoolid
        // history_id  30,20,1,4,24,89,78
        // $schoolId = 1;
        $history_id = $_COOKIE['history_id'];

        $id = request("schoolId","");

        $history_id = explode(',', $history_id);
        $index = array_search($id, $history_id);

        if($index || $index === 0){//判断id是否存在
            unset($history_id[$index]);
        }else if(count($history_id) == 7){
            unset($history_id[6]);
        }
        if(implode(',', $history_id) == ""){// 第一次setcookie
            $history_id = $id;
        }else{
            $history_id = $id . ',' . implode(',', $history_id);
        }

        setcookie('history_id' , $history_id , time() + 3600*24*90,'/');//设置3个月cookie

        $schoolId = intval(request("schoolId",""));
        $where = "collegeId = " . $schoolId;
        $collegeinfo = $this->mcollegeinfo_detail->detail_get_one("#@__collegeinfo" , $where);// 学校信息
        if(!empty($collegeinfo)){

            $collegeinfo_detail = $this->mcollegeinfo_detail->detail_get_one("#@__collegeinfo_detail" , $where);// 学校信息细节
            $collegeinfo_news = $this->mcollegeinfo_detail->detail_get_list("#@__collegeinfo_news " , $where);// 学校相关新闻
            $collegeinfo_project = $this->mcollegeinfo_detail->detail_get_list("#@__collegeinfo_project " , $where);// 项目特色
            $collegeinfo_recruit = $this->mcollegeinfo_detail->detail_get_list("#@__collegeinfo_recruit" , $where . " order by id DESC");//  历年招生数据
            $collegeinfo_professor = $this->mcollegeinfo_detail->detail_get_list("#@__collegeinfo_professor" , $where . " order by id DESC");//  历年招生数据
            $contact = $this->mcollegeinfo_detail->detail_get_list("#@__contact" , "1");//  联系方式
            $collegeinfo_graduate = $this->mcollegeinfo_detail->detail_get_list("#@__collegeinfo_graduate" ,$where . " order by id DESC");//  联系方式
            //预处理变量
            $area_code = $collegeinfo['area'];
            if(!empty($collegeinfo['area'])){
                $collegeinfo['area'] = GetEnumsValue("area",intval($collegeinfo['area']));
            }
            if(!empty($collegeinfo['level'])){
                $collegeinfo['level'] = explode(',', $collegeinfo['level']);
                foreach ($collegeinfo['level'] as $k => $v) {
                    $collegeinfo['level'][$k] = GetEnumsValue('level',intval($v));
                }
                $collegeinfo['level'] = implode(',', $collegeinfo['level']);
            }
            if(!empty($collegeinfo['scoreType'])){
                $collegeinfo['scoreType'] = GetEnumsValue("scoreType",intval($collegeinfo['scoreType']));
            }
            if(!empty($collegeinfo['projectType'])){
                $collegeinfo['projectType'] = explode(',', $collegeinfo['projectType']);
                foreach ($collegeinfo['projectType'] as $k => $v) {
                    $collegeinfo['projectType'][$k] = GetEnumsValue('projectType',intval($v));
                }
                $collegeinfo['projectType'] = implode(',', $collegeinfo['projectType']);
            }

            $this->SetVar("collegeinfo" , $collegeinfo);
            $this->SetVar("collegeinfo_detail" , $collegeinfo_detail);
            $this->SetVar("collegeinfo_news" , $collegeinfo_news);
            $this->SetVar("collegeinfo_project" , $collegeinfo_project);
            $this->SetVar("collegeinfo_recruit" , $collegeinfo_recruit);
            $this->SetVar("collegeinfo_professor" , $collegeinfo_professor);
            $this->SetVar("history_college" , $history_college);
            $this->SetVar("recommend_college" , $recommend_college);
            $this->SetVar("area_code" , $area_code);
            $this->SetVar("contact" , $contact);
            $this->SetVar("schoolId" , $schoolId);
            $this->SetVar("collegeinfo_graduate" , $collegeinfo_graduate);

            $this->SetTemplate('collegeinfo_detail.htm');
            $this->Display();
        }else{
            showMsg("查询院校不存在！","http://www.guanjunedu.com");
            exit();
        }

    }

    /**
     * 根据最近浏览院校的推荐函数，根据最多的学费范围，地区推荐
     * $history_college  最近浏览院校数组
     * return  推荐院校的数组
     * by HehongChen
     * 2014.10.30
     */
    function CollegeRecommend($history_ids){
        $history_ids_str = join(',',$history_ids);
        // 获取院校中最多的学费范围和地区
        $sql_max_feerange = "SELECT feeRange FROM `#@__collegeinfo` WHERE collegeId IN ($history_ids_str) group by feeRange order by count(feeRange) desc limit 1";
        $this->dsql->SetQuery($sql_max_feerange);
        $this->dsql->Execute();
        $max_feerange_arr = $this->dsql->GetArray();
        $max_feerange = $max_feerange_arr['feeRange'];

        $sql_max_area = "SELECT area FROM `#@__collegeinfo` WHERE collegeId IN ($history_ids_str) group by area order by count(area) desc limit 1";
        $this->dsql->SetQuery($sql_max_area);
        $this->dsql->Execute();
        $max_area_arr = $this->dsql->GetArray();
        $max_area = $max_area_arr['area'];

        // // 构造查询的sql语句
        $sql = "SELECT * FROM `#@__collegeinfo` WHERE  feeRange=$max_feerange AND area=$max_area AND collegeId NOT IN ($history_ids_str)";
        $this->dsql->SetQuery($sql);
        $this->dsql->Execute();
        $recommend_college = array();
        while($row = $this->dsql->GetArray()){
            $recommend_college[] = $row;
        }

        return $recommend_college;
    }

    function ac_news(){
            $pv = new PartView();
            $pv->SetTemplet(DEDEWENDA . "/templates/default/mbanews.htm");
            $pv->Display();
    }

    function ac_school_detail(){

            // 处理cookie获取最近浏览和推荐院校的数组
            // by HehongChen
            // 2014.10.31
            // 通过cookie获取最近浏览的院校的id
            $history_id_pre = rtrim($_COOKIE['history_id'],',');
            $history_ids = explode(',', $history_id_pre);
            $history_ids_str = join(',',$history_ids);
            // 获取最佳浏览的院校的数组
            $sql = "SELECT * FROM `#@__collegeinfo` WHERE  collegeId IN ($history_ids_str)";
            // var_dump($this->dsql);exit();
            $this->dsql->SetQuery($sql);
            $this->dsql->Execute();
            $history_college = array();
            while($row = $this->dsql->GetArray()){
                $history_college[] = $row;
            }
            // 推荐院校的数组
            $recommend_college = self::CollegeRecommend($history_ids);

            $schoolId = intval(request("schoolId",""));
            $where = "collegeId = " . $schoolId;
            $collegeinfo = $this->mcollegeinfo_detail->detail_get_one("#@__collegeinfo" , $where);// 学校信息
            $collegeinfo_detail = $this->mcollegeinfo_detail->detail_get_one("#@__collegeinfo_detail" , $where);// 学校信息细节
            $contact = $this->mcollegeinfo_detail->detail_get_list("#@__contact" , "1");//  联系方式

            $area_code = $collegeinfo['area'];
            if(!empty($collegeinfo['area'])){
                $collegeinfo['area'] = GetEnumsValue("area",intval($collegeinfo['area']));
            }
            $this->SetVar("collegeinfo" , $collegeinfo);
            $this->SetVar("collegeinfo_detail" , $collegeinfo_detail);
            $this->SetVar("contact" , $contact);
            $this->SetVar("schoolId" , $schoolId);
            $this->SetVar("history_college" , $history_college);
            $this->SetVar("recommend_college" , $recommend_college);

            $this->SetTemplet("school_detail.htm");
            $this->Display();
    }

    function ac_quick_register(){
        $email = request("email","");
        $username = request("username","");
        $phone = request("phone","");
        $degree = request("degree","");
        $schoolGraduate = request("schoolGraduate","");
        $dateGraduate = request("dateGraduate","");
        $addTime = $updateTime = date("Y-m-d H:i:s",time());
        $this->mcollegeinfo_detail->detail_query("insert into `#@__guanjun_register` (email,userName,phone,dateGraduate,schoolGraduate,education,schoolChoose,addTime,updateTime)
                values ('$email','$username','$phone','$dateGraduate','$schoolGraduate','$degree','','$addTime','$updateTime')");
    }

}
?>